package com.chenglai.article.controller;

import com.chenglai.article.pojo.ArticleCategory;
import com.chenglai.article.pojo.ArticleInfo;
import com.chenglai.article.service.ArticleInfoService;
import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：文章
 *
 * @Author rxh
 * @Date 2023/11/15 15:12
 */
@RestController
@RequestMapping("/articleInfo/")
public class ArticleInfoController {

    @Resource
    private ArticleInfoService articleInfoService;

    /**
     * 获取文章列表
     *
     * @param page 页码
     * @param size 数量
     * @param shopId 门店id
     * @return
     */
    @PostMapping("getArticleInfoList")
    @ApiOperation(value = "获取文章列表")
    public Result getArticleInfoList(Integer page, Integer size, Integer shopId) {
        try {
            PageInfo pageInfo = articleInfoService.getArticleInfoList(page, size, shopId);
            return new Result(true, StatusCode.OK, "操作成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 添加/修改文章
     * @param articleInfo 文章实体类
     */
    @PostMapping("editArticleInfo")
    @ApiOperation(value = "添加/修改文章")
    public Result editArticleCategory(ArticleInfo articleInfo) {
        try {
            articleInfoService.editArticleInfo(articleInfo);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 删除文章
     * @param articleId 文章id
     */
    @PostMapping("delArticleInfo")
    @ApiOperation(value = "删除文章")
    public Result delArticleCategory(Integer articleId) {
        try {
            articleInfoService.delArticleInfo(articleId);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 修改排序
     * @param articleId 文章id
     * @param sort 排序
     */
    @PostMapping("updateSortByArticleId")
    @ApiOperation(value = "修改排序")
    public Result updateSortByArticleId(Integer articleId, Integer sort) {
        try {
            articleInfoService.updateSortByArticleId(articleId, sort);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 修改显示状态
     * @param articleId 文章id
     * @param isDelete 状态 0已删 1未删
     */
    @PostMapping("updateIsDeleteByArticleId")
    @ApiOperation(value = "修改显示状态")
    public Result updateIsDeleteByArticleId(Integer articleId, Integer isDelete) {
        try {
            articleInfoService.updateIsDeleteByArticleId(articleId, isDelete);
            return new Result(true, StatusCode.OK, "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "操作失败");
        }
    }

    /**
     * 文章详情
     * @param articleId 文章id
     */
    @PostMapping("detail")
    @ApiOperation(value = "文章详情")
    public Result detail(Integer articleId) {
        try {
            ArticleInfo detail = articleInfoService.detail(articleId);
            return new Result(true, StatusCode.OK, "查询成功", detail);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }

    /**
     * 查询所有文章信息包含分类信息
     *
     * @param shopId 门店id
     * @return
     */
    @PostMapping("articleAll")
    @ApiOperation(value = "查询所有文章信息包含分类信息")
    public Result articleAll(Integer shopId) {
        try {
            List<ArticleCategory> list = articleInfoService.articleAll(shopId);
            return new Result(true, StatusCode.OK, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
}
